<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="qunit/jquery.min.js"></script>
  <link rel="stylesheet" href="qunit/qunit.css" type="text/css" media="screen" />
  <script type="text/javascript" src="qunit/qunit.js"></script>
  <script type="text/javascript" src="pyscript.js"></script>
  
</head>
<body>
  <h1 id="qunit-header">Pyscript Unit Tests</h1>
 <h2 id="qunit-banner"></h2>
 <h2 id="qunit-userAgent"></h2>
 <ol id="qunit-tests"></ol>
 <script> 
 
module("Line Rules"); 

test( "function", function() {
  var lineRule = Pyscript.getLineRule("function");

  var  input = "function plus(a,b):";
  var expect = "function plus(a,b){";
  equals( lineRule.execute(input), expect );
  var padding = "\t  \t";
  var input  = padding + input;
  var expect = padding + expect;
  equals( lineRule.execute(input), expect );
});

test( "if", function() {
  var lineRule = Pyscript.getLineRule("if");
  
  var  input = "if 5 == 2+3:";
  var expect = "if( 5 == 2+3 ){";
  equals( lineRule.execute(input), expect );
});

test( "for", function() {
  var lineRule = Pyscript.getLineRule("for");
  
  var  input = "for var i=0; i<5; i++:";
  var expect = "for( var i=0; i<5; i++ ){";
  equals( lineRule.execute(input), expect );
});

test( "inline ability", function() {
  var lineRule = Pyscript.getLineRule("if");
  var  input = "if 5 == 2+3: console.log('yep')";
  var expect = "if( 5 == 2+3 ){ console.log('yep') }";
  equals( lineRule.execute(input), expect );
  
  var lineRule = Pyscript.getLineRule("for");
  var  input = "for var i=0;i<nums.length;i++: sum+=nums[i]";
  var expect = "for( var i=0;i<nums.length;i++ ){ sum+=nums[i] }";
  equals( lineRule.execute(input), expect );
  
  var lineRule = Pyscript.getLineRule("function");
  var  input = "function times(a,b): return a*b";
  var expect = "function times(a,b){ return a*b }";
  equals( lineRule.execute(input), expect );
});


module("Functions");

test("single line", function() {
  var  input = "function plus(a,b):\n\treturn a+b"
  var expect = "function plus(a,b){\n\treturn a+b}";
  equals( Pyscript.compile(input), expect );
  
  input  += "\n";
  expect = "function plus(a,b){\n\treturn a+b\n}";

  equals( Pyscript.compile(input), expect );
});

test("initial whitespace/predent", function() {
  var  input = "  if true: var a=5\n\n  if true: var a=5"
  var expect = "  if( true ){ var a=5 }\n\n  if( true ){ var a=5 }"
  equals( Pyscript.getPredentFromLines(input.split("\n")), "  ", "Predent got correctly" );  
  equals( Pyscript.compile(input), expect );

  var  input = "\tfunction plus(a,b):\n\t\treturn a+b"
  var expect = "\tfunction plus(a,b){\n\t\treturn a+b}";
  equals( Pyscript.getPredentFromLines(input.split("\n")), "\t", "Predent" );
  equals( Pyscript.compile(input), expect );

  var  input = "\n\tfunction plus(a,b):\n\t\treturn a+b"
  var expect = "\n\tfunction plus(a,b){\n\t\treturn a+b}";
  equals( Pyscript.getPredentFromLines(input.split("\n")), "\t", "Predent" );
  equals( Pyscript.compile(input), expect );  
});

test("Inline function", function(){
  var  input = "function plus(a,b): return a+b";
  var expect = "function plus(a,b){ return a+b }";
  equals( Pyscript.compile(input), expect );  

  var  input = "function plus(a,b): return a+b\nplus(1,2)";
  var expect = "function plus(a,b){ return a+b }\nplus(1,2)"
  equals( Pyscript.compile(input), expect );  
});

test("Anonymous inline functions", function(){
  var  input = "twice(3, function times5(x): return x*5)";
  var expect = "twice(3, function times5(x){ return x*5 })";
  equals( Pyscript.compile(input), expect );
  
  var  input = "twice(3, function times5(x):\n\treturn x*5\n)"; 
  var expect = "twice(3, function times5(x){\n\treturn x*5\n})";
  equals( Pyscript.compile(input), expect );

});

test("Inline function inside another function", function() {
  var  input = "function triangle(a,b):\n\tfunction sqroot(x): return Math.pow(x,.5)\n\treturn sqroot( a*a + b*b )"
  var expect = "function triangle(a,b){\n\tfunction sqroot(x){ return Math.pow(x,.5) }\n\treturn sqroot( a*a + b*b )}"
  equals( Pyscript.compile(input), expect );  
});

module("For loop");

test( "For loop with a if inside", function(){
  var  input = "var count = 0\nfor var i=0; i<5; i++:\n\tcount += 1\n\tif count==2:\n\t\tcount *= 5";
  var expect = "var count = 0\nfor( var i=0; i<5; i++ ){\n\tcount += 1\n\tif( count==2 ){\n\t\tcount *= 5}}"
  equals( Pyscript.compile(input), expect );  
});
 
 </script>
</body>
</html>
